import 'package:flutter/cupertino.dart';
import 'package:mvvm_provider/page/collection/data/repository/novel_db_manager.dart';
import 'package:mvvm_provider/page/home/data/model/cartoon_model.dart';
import 'package:mvvm_provider/until/app_util.dart';

class HandleDataViewModel extends ChangeNotifier {
  List<CartoonModel> dataList = [];

  /// 刷新数据
  Future<void> updateData() async {
    List<CartoonModel>? list = await NovelDbManager().queryAllData();
    if (AppUtil.isEmpty(list)) return;
    dataList.clear();
    dataList.addAll(list!);
    notifyListeners();
  }

  /// 收藏
  Future<void> collectionNovel(CartoonModel model) async {
    await NovelDbManager().insert(model);
    updateData();
  }

  /// 取消收藏
  Future<void> cancelCollectionNovel(String itemId) async {
    await NovelDbManager().delete(itemId);
    updateData();
  }
}
